package com.cdn.nodes.service.impl;

import com.cdn.common.core.exception.ServiceException;
import com.cdn.common.core.utils.DateUtils;
import com.cdn.nodes.domain.EdgeIpList;
import com.cdn.nodes.domain.EdgeIpNode;
import com.cdn.nodes.domain.dto.EdgeIpListAddDTO;
import com.cdn.nodes.mapper.EdgeIpListMapper;
import com.cdn.nodes.mapper.EdgeIpNodeMapper;
import com.cdn.nodes.service.IEdgeIpListService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.sql.rowset.serial.SerialException;
import java.util.List;

/**
 * IP名单，记录允许或禁止访问网站的IP地址Service业务层处理
 * 
 * @author Cinzia
 * @date 2024-12-20
 */
@Service
public class EdgeIpListServiceImpl implements IEdgeIpListService
{
    @Resource
    private EdgeIpListMapper edgeIpListMapper;

    @Resource
    private EdgeIpNodeMapper edgeIpNodeMapper;

    /**
     * 查询IP名单，记录允许或禁止访问网站的IP地址
     * 
     * @param id IP名单，记录允许或禁止访问网站的IP地址主键
     * @return IP名单，记录允许或禁止访问网站的IP地址
     */
    @Override
    public EdgeIpList selectEdgeIpListById(Long id)
    {
        return edgeIpListMapper.selectEdgeIpListById(id);
    }

    /**
     * 查询IP名单，记录允许或禁止访问网站的IP地址列表
     * 
     * @param edgeIpList IP名单，记录允许或禁止访问网站的IP地址
     * @return IP名单，记录允许或禁止访问网站的IP地址
     */
    @Override
    public List<EdgeIpList> selectEdgeIpListList(EdgeIpList edgeIpList)
    {
        return edgeIpListMapper.selectEdgeIpListList(edgeIpList);
    }

    /**
     * 新增IP名单，记录允许或禁止访问网站的IP地址
     * 
     * @param edgeIpListAddDTO IP名单，记录允许或禁止访问网站的IP地址
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public int insertEdgeIpList(EdgeIpListAddDTO edgeIpListAddDTO)
    {
        EdgeIpList edgeIpList = new EdgeIpList();
        BeanUtils.copyProperties(edgeIpListAddDTO, edgeIpList);

        int result = edgeIpListMapper.insertEdgeIpList(edgeIpList);
        if (result > 0) {
            Long ipId = edgeIpList.getId();

            EdgeIpNode edgeIpNode = new EdgeIpNode();
            edgeIpNode.setIpAddressId(ipId);
            edgeIpNode.setNodeId(edgeIpListAddDTO.getNodeId());

            // 插入edgeIpNode到中间表
            edgeIpNodeMapper.insertEdgeIpNode(edgeIpNode);
        }

        return result;
    }

    /**
     * 修改IP名单，记录允许或禁止访问网站的IP地址
     * 
     * @param edgeIpList IP名单，记录允许或禁止访问网站的IP地址
     * @return 结果
     */
    @Override
    public int updateEdgeIpList(EdgeIpList edgeIpList)
    {
        edgeIpList.setUpdateTime(DateUtils.getNowDate());
        return edgeIpListMapper.updateEdgeIpList(edgeIpList);
    }

    /**
     * 批量删除IP名单，记录允许或禁止访问网站的IP地址
     * 
     * @param ids 需要删除的IP名单，记录允许或禁止访问网站的IP地址主键
     * @return 结果
     */
    @Override
    public int deleteEdgeIpListByIds(Long[] ids)
    {
        return edgeIpListMapper.deleteEdgeIpListByIds(ids);
    }

    /**
     * 删除IP名单，记录允许或禁止访问网站的IP地址信息
     * 
     * @param id IP名单，记录允许或禁止访问网站的IP地址主键
     * @return 结果
     */
    @Override
    public int deleteEdgeIpListById(Long id)
    {
        return edgeIpListMapper.deleteEdgeIpListById(id);
    }

    @Override
    public String getIpIdByNodeId(Long nodeId) {
        Long ipAddressId = edgeIpNodeMapper.selectIpAddressId(nodeId);
        EdgeIpList edgeIpList = edgeIpListMapper.selectEdgeIpListById(ipAddressId);
        return edgeIpList.getIpAddress();
    }

}
